<?php

header('Content-type: application/json');

class ReturnValues {

    public $success = "";
    public $errorcode = "";
    public $videolink = "";

}

$returnValues = new ReturnValues();
$returnValues->success = false;
$returnValues->errorcode = 3;
$returnValues->videolink = "Script verlangt andere Parameter";




if (isset($_POST['linkToAdd'])) {


    $vidID = getVideoID($_POST['linkToAdd']);

    if ($vidID != -1) {

        //echo "<p>Video-ID: " . $vidID . "</p>";



            $vidExists = doesVideoExists($vidID);



            if ($vidExists) {

                $videoToShow = insertToDB($vidID);

                //echo "<p>Video-ID: " . $vidID . "</p>";
                //echo "<p>Video to show: " . $videoToShow . "</p>";

                $returnValues->success = true;
                $returnValues->errorcode = 0;
                $returnValues->videolink = $videoToShow;
            } else {

                $returnValues->success = false;
                $returnValues->errorcode = 2;
                $returnValues->videolink = "Es existiert kein YouTube-Video mit dieser ID";
                //echo "YouTube-Video existiert nicht.";
            }
        } else {
            $returnValues->success = false;
            $returnValues->errorcode = 1;
            $returnValues->videolink = "Link ist kein YouTube-Video";
            //echo "Kein Youtube Link";
        }

}



print json_encode($returnValues);


function getVideoID($url) {

    //$start = microtime(true);

    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
        //$end = microtime(true);
        //$laufzeit = $end - $start;
        //echo 'Laufzeit Parsen: ' . $laufzeit . 'Sekunden <br>';

        return $match[1];
    } else if (preg_match('%(youtu.be/)', $url, $match)) {

        //$end = microtime(true);
        //$laufzeit = $end - $start;
        //echo 'Laufzeit Parsen: ' . $laufzeit . 'Sekunden <br>';
        return $match[1];
    } else {

        //$end = microtime(true);
        //$laufzeit = $end - $start;
        //echo 'Laufzeit Parsen: ' . $laufzeit . 'Sekunden <br>';
        return -1;
    }
}

function startsWith($haystack, $needle)
{
    return $needle === "" || strpos($haystack, $needle) === 0;
}

function doesVideoExists($videoID) {


    //$start = microtime(true);


    $devKey = 'AI39si6jc3KqqnKhgTRLfIk3lMRSw-rwUfrhpP4Wh6Hfq_VZZU5wlIGW--nZAPFq-d0Injvemqa0y-YEy-W5ckabiRiZzHEGaA';


    $xml = file_get_contents('http://gdata.youtube.com/feeds/api/videos/' . $videoID . '?key=' . $devKey);
    if (startsWith($xml, '<?xml')) {

        //$end = microtime(true);
        //$laufzeit = $end - $start;
        //echo 'Laufzeit ID Check: ' . $laufzeit . 'Sekunden <br>';


        return true;
    } else {

        //$end = microtime(true);
        //$laufzeit = $end - $start;
        //echo 'Laufzeit ID Check: ' . $laufzeit . 'Sekunden <br>';
        return false;
    }
}

function insertToDB($videoToAdd) {
    //echo '<br> <br>Database Times:';
    //$time1 = microtime(true);

    $con = mysql_connect("rdbms.strato.de", "U1453433", "shaggy49") or die("Unable to connect to MySQL");
    //$time2 = microtime(true);
    //$dif = $time2 - $time1;
    //echo '<br>ConnectServer: ' . $dif;

    $selected = mysql_select_db("DB1453433", $con) or die("Could not select examples");
    //$time3 = microtime(true);
    //$dif = $time3 - $time2;
    //echo '<br>SelectDB: ' . $dif;

    $result = mysql_query("SELECT * FROM LINKS ORDER BY RAND() LIMIT 1");
    $row = mysql_fetch_row($result);

    $videoToShow = $row[1];
    //$time4 = microtime(true);
    //$dif = $time4 - $time3;
    //echo '<br>Select Random Video: ' . $dif;

    //mysql_query("DELETE FROM LINKS WHERE LinkID = $row[0]");
    //$time5 = microtime(true);
    //$dif = $time5 - $time4;
    //echo '<br>Delete Randomed Video: ' . $dif;
    mysql_query("INSERT INTO LINKS (Link) VALUES ('$videoToAdd')");
    //$time6 = microtime(true);
    //$dif = $time6 - $time5;
    //echo '<br>Insert New Video: ' . $dif;

    mysql_close($con);
    //$time7 = microtime(true);
    //$dif = $time7 - $time6;
    //echo '<br>Close DB-Connection: ' . $dif;
    //$end = microtime(true);
    //$laufzeit = $end - $time1;
    //echo '<br>Laufzeit DataBase-All: ' . $laufzeit . '<br>';

    return $videoToShow;
}

?>
